import java.util.Arrays;

// Definition for singly-linked list.
class ListNode {
    int val;
    ListNode next;
      ListNode(int x) {
         val = x;
         next = null;
      }
 }
public class GetIntersectionNode {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode A = headA, B = headB;
        while (A != B) {
            A = A != null ? A.next : headB;
            B = B != null ? B.next : headA;
        }
        return A;
    }
}
